package school.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import school.domain.Category;
import school.domain.Doc;
import school.domain.User;
import school.mapper.CategoryMapper;
import school.mapper.DocMapper;
import school.mapper.UserMapper;
import school.support.BaseController;
import school.support.pojo.DocPo;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@RequestMapping("doc")
@Controller
public class DocController extends BaseController {

    @Autowired
    private DocMapper docMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 列表
     *
     * @param doc
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(DocPo doc, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        Example example = new Example(Doc.class);
        Example.Criteria criteria = example.createCriteria();
        if (doc.getName() != null) {
            criteria.andCondition("d.name like '%" + doc.getName() + "%'");
        }

        if (doc.getNo() != null) {
            criteria.andCondition("d.no like '%" + doc.getNo() + "%'");
        }

        if (doc.getCategoryname() != null) {
            criteria.andCondition("c.name like '%" + doc.getCategoryname() + "%'");
        }

        if (doc.getTeachername() != null) {
            criteria.andCondition("u.name like '%" + doc.getTeachername() + "%'");
        }

        PageHelper.startPage(pageNum, pageSize);
        PageInfo<DocPo> pageInfo = new PageInfo<>(docMapper.selectPoByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "doc/list";
    }

    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Doc doc = docMapper.selectByPrimaryKey(id);
        model.addAttribute(doc);
       /* List<Category> categoryList = categoryMapper.selectAll();
        model.addAttribute("categoryList", categoryList);*/
        Example example = new Example(User.class);
        example.createCriteria().andNotEqualTo("type", "admin");
        List<User> userList = userMapper.selectByExample(example);
        model.addAttribute("userList", userList);
        return "doc/detail";
    }

    /**
     * 修改信息
     *
     * @param doc
     * @return
     */
    @RequestMapping("update")
    public String update(Doc doc, MultipartFile file) {
        Doc temp = docMapper.selectByPrimaryKey(doc.getId());
        doc.setOldpath(temp.getPath());
        if (!file.isEmpty()) {
            String path = saveFile(file);
            doc.setPath(path);
        }
        docMapper.updateByPrimaryKeySelective(doc);
        return refresh();
    }

    @GetMapping("add")
    public String add(Model model) {
        Example example = new Example(Category.class);
        example.createCriteria().andEqualTo("pid", 0);
        List<Category> categoryList = categoryMapper.selectByExample(example);
        model.addAttribute("categoryList", categoryList);
        example = new Example(User.class);
        example.createCriteria().andNotEqualTo("type", "admin");
        List<User> userList = userMapper.selectByExample(example);
        model.addAttribute("userList", userList);
        return "doc/add";
    }

    @PostMapping("add")
    public String add(Doc doc, MultipartFile file) {
        if (!file.isEmpty()) {
            String path = saveFile(file);
            doc.setPath(path);
        }
        doc.setAddtime(new Date());
        docMapper.insertSelective(doc);
        if (sessionUser().getType().equals("admin"))
            return "redirect:list";
        else return "redirect:mylist";
    }

    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        docMapper.deleteByPrimaryKey(id);
        return refresh();
    }

    /**
     * 删除旧文档
     *
     * @param id
     * @return
     */
    @RequestMapping("delold/{id}")
    public String delold(@PathVariable Integer id) {
        Doc doc = docMapper.selectByPrimaryKey(id);
        doc.setOldpath("");
        docMapper.updateByPrimaryKey(doc);
        return refresh();
    }

    /**
     * 我管理的文档
     *
     * @param doc
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("mylist")
    public String mylist(DocPo doc, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        Example example = new Example(Doc.class);
        Example.Criteria criteria = example.createCriteria();
        if (doc.getName() != null) {
            criteria.andCondition("d.name like '%" + doc.getName() + "%'");
        }
        if (doc.getNo() != null) {
            criteria.andCondition("d.no like '%" + doc.getNo() + "%'");
        }

        if (doc.getCategoryname() != null) {
            criteria.andCondition("c.name like '%" + doc.getCategoryname() + "%'");
        }
        criteria.andEqualTo("userid", sessionUser().getId());
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<DocPo> pageInfo = new PageInfo<>(docMapper.selectPoByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "doc/mylist";
    }


    /**
     * 按照年份查看
     * @param year
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("mylistN")
    public String mylistN(@RequestParam(defaultValue = "2018") String year, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        Example example = new Example(Doc.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andCondition("d.no like '%" + year + "%'");

        criteria.andEqualTo("userid", sessionUser().getId());
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<DocPo> pageInfo = new PageInfo<>(docMapper.selectPoByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "doc/mylistN";
    }
}
